package com.ruoyi.project.income.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.income.domain.BinsMerchantRecode;
import com.ruoyi.project.income.service.IBinsMerchantRecodeService;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.income.mapper.BinsMerchantAccountMapper;
import com.ruoyi.project.income.domain.BinsMerchantAccount;
import com.ruoyi.project.income.service.IBinsMerchantAccountService;

/**
 * 商户账户信息Service业务层处理
 * 
 * @author admin
 * @date 2025-04-03
 */
@Service
public class BinsMerchantAccountServiceImpl implements IBinsMerchantAccountService 
{
    private Logger logger = LoggerFactory.getLogger(BinsMerchantAccountServiceImpl.class);
    @Autowired
    private BinsMerchantAccountMapper binsMerchantAccountMapper;

    /**
     * 查询商户账户信息
     * 
     * @param merchantDeptId 商户账户信息主键
     * @return 商户账户信息
     */
    @Override
    public BinsMerchantAccount selectBinsMerchantAccountByMerchantDeptId(Long merchantDeptId)
    {
        return binsMerchantAccountMapper.selectBinsMerchantAccountByMerchantDeptId(merchantDeptId);
    }

    /**
     * 查询商户账户信息列表
     * 
     * @param binsMerchantAccount 商户账户信息
     * @return 商户账户信息
     */
    @Override
    public List<BinsMerchantAccount> selectBinsMerchantAccountList(BinsMerchantAccount binsMerchantAccount)
    {
        return binsMerchantAccountMapper.selectBinsMerchantAccountList(binsMerchantAccount);
    }

    /**
     * 新增商户账户信息
     * 
     * @param binsMerchantAccount 商户账户信息
     * @return 结果
     */
    @Override
    public int insertBinsMerchantAccount(BinsMerchantAccount binsMerchantAccount)
    {
        binsMerchantAccount.setCreateTime(DateUtils.getNowDate());
        return binsMerchantAccountMapper.insertBinsMerchantAccount(binsMerchantAccount);
    }

    @Autowired
    IBinsMerchantRecodeService binsMerchantRecodeService;

    /**
     * 修改商户账户信息
     * 
     * @param binsMerchantAccount 商户账户信息
     * @return 结果
     */
    @Override
    public int updateBinsMerchantAccount(BinsMerchantAccount binsMerchantAccount)
    {
        if (Objects.isNull(binsMerchantAccount.getMerchantDeptId())){
            return 0;
        }
        BinsMerchantAccount binsMerchantAccount_data = binsMerchantAccountMapper.selectBinsMerchantAccountByMerchantDeptId(binsMerchantAccount.getMerchantDeptId());
        logger.info("binsMerchantAccount_data:{}",binsMerchantAccount_data);
        if (Objects.isNull(binsMerchantAccount_data)){
            return binsMerchantAccountMapper.insertBinsMerchantAccount(binsMerchantAccount);
        }
        if (Objects.nonNull(binsMerchantAccount.getWithdrawal())){
            if (Objects.isNull(binsMerchantAccount_data.getAvailable())){
                throw new RuntimeException("可用余额为空");
            }
            // 提现记录
            if (binsMerchantAccount_data.getAvailable().compareTo(binsMerchantAccount.getWithdrawal()) < 0){
                throw new RuntimeException("提现金额大于可用余额");
            }
            BinsMerchantRecode binsMerchantRecode = new BinsMerchantRecode();
            binsMerchantRecode.setMerchantDeptId(binsMerchantAccount.getMerchantDeptId());
            binsMerchantRecode.setAccountNum(binsMerchantAccount_data.getAccountNum());
            binsMerchantRecode.setCashOutAmount(binsMerchantAccount.getWithdrawal());
            binsMerchantRecode.setCreateTime(DateUtils.getNowDate());
            binsMerchantRecode.setCreateBy(SecurityUtils.getUsername());
            int count = binsMerchantRecodeService.insertBinsMerchantRecode(binsMerchantRecode);
            if (count == 0){
                throw new RuntimeException("提现失败");
            }
            binsMerchantAccount.setUpdateTime(DateUtils.getNowDate());
            BinsMerchantAccount binsMerchantAccount_update = new BinsMerchantAccount();
            binsMerchantAccount_update.setMerchantDeptId(binsMerchantAccount.getMerchantDeptId());
            binsMerchantAccount_update.setAvailable(binsMerchantAccount_data.getAvailable().subtract(binsMerchantAccount.getWithdrawal()));
            binsMerchantAccount_update.setUpdateTime(DateUtils.getNowDate());
            return binsMerchantAccountMapper.updateBinsMerchantAccount(binsMerchantAccount_update);
        }

        binsMerchantAccount.setUpdateTime(DateUtils.getNowDate());
        return binsMerchantAccountMapper.updateBinsMerchantAccount(binsMerchantAccount);
    }

    /**
     * 批量删除商户账户信息
     * 
     * @param merchantDeptIds 需要删除的商户账户信息主键
     * @return 结果
     */
    @Override
    public int deleteBinsMerchantAccountByMerchantDeptIds(Long[] merchantDeptIds)
    {
        return binsMerchantAccountMapper.deleteBinsMerchantAccountByMerchantDeptIds(merchantDeptIds);
    }

    /**
     * 删除商户账户信息信息
     * 
     * @param merchantDeptId 商户账户信息主键
     * @return 结果
     */
    @Override
    public int deleteBinsMerchantAccountByMerchantDeptId(Long merchantDeptId)
    {
        return binsMerchantAccountMapper.deleteBinsMerchantAccountByMerchantDeptId(merchantDeptId);
    }
}
